// pages/shopIndex/shopIndex.js
var fileData = require('../../utils/data.js');
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // banner_url: fileData.getBannerData(),
    imageUrl: app.globalData.imageUrl,
    userInfo: '',
    starNum:5,
    focus:'',
    showSelect:false,
    bottabDatadetail: app.globalData.bottabForPpsjindex,
    viewtabList:[
      { icon: 'icon-dianpushouye', txt: '店铺首页', current: true},
      { icon: 'icon-quanbushangpin', txt: '全部商品', current: false },
      { icon: 'icon-xinpinshangjia', txt: '新品上架', current: false },
      { icon: 'icon-dianpujieshao', txt: '店铺介绍', current: false}
    ],
    commodity:[],
    actiItem: [],
    shopInfo:'',
    shopArea: '',
    id:'',
    qrcode:true,
    isMine: 0,
    isCollect: 'N',
    collectId:'',
    dateFlag: '',
    fee: '',
    loadSuccess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading()
    this.setData({
      ['bottabDatadetail[0].current']: true,
      isShop: wx.getStorageSync("isShop"),
      ShopId: wx.getStorageSync("ShopId"),
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      id: options.id
    })
    if (options.id == this.data.ShopId){
      this.setData({
        isMine: 1
      })
    }
    if (options.whole == 1){
      this.setData({
        id: wx.getStorageSync("ShopId"),
        isMine: 1
      })
    }
    this.getbusinessList(this.data.id);
    this.getIndexgoods(this.data.id);
  },
  switchBotab(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url + '?shopId=' + this.data.id + '&isMine=' + this.data.isMine + '&area=' + this.data.shopArea + '&id=' + this.data.id
    })
  },
  openList: function () {
    wx.navigateTo({
      url: '/pages/goodList/goodList?shopId='+this.data.id
    })
  },
  // 根据id获取商铺信息
  getbusinessList:function(id){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'shopInfo/getOne',
      data: {
        id: id,
        status: 2,
        loginUserId: _this.data.userInfo.id
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        _this.setData({
          loadSuccess: true
        })
        wx.hideLoading()
        if(res.data.success){
          let key = 'shopInfo.realOpenConfirm';
          let key1 = 'shopInfo.realProductsImg';
          let key2 = 'shopInfo.realShopBanner';
          let key3 = 'shopInfo.realShopLogo';
          let key4 = 'shopInfo.realShopQrCode';
          _this.setData({
            isCollect: res.data.data.collectionFlag,
            dateFlag: res.data.data.dateFlag,
            shopInfo: res.data.data.data,
            shopArea: res.data.data.data.region,
            [key]: res.data.data.data.realOpenConfirm.split(","),
            [key1]: res.data.data.data.realProductsImg.split(","),
            [key2]: res.data.data.data.realShopBanner.split(","),
            [key3]: res.data.data.data.realShopLogo.split(","),
            [key4]: res.data.data.data.realShopQrCode.split(",")
          })
        }
      }
    })
  },
  inputFee: function(e){
    this.setData({
      fee: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
    })
  },
  // 去买单
  pay: function(){
    this.setData({
      qrcode:false
    })
  },
  payMoney: function () {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'shopOrder/payTheBill',
      data: {
        openId: app.globalData.openId,
        userId: that.data.userInfo.id,
        backupE: that.data.id,
        backupC: 5,
        money: that.data.fee,
        body: '线下支付费用'
      },
      success: function (res) {
        if (res.data.success) {
          that.requestPayment(res.data.data);
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  requestPayment: function (obj) {
    var that = this;
    wx.requestPayment({
      'timeStamp': obj.timeStamp,
      'nonceStr': obj.nonceStr,
      'package': obj.package,
      'signType': obj.signType,
      'paySign': obj.paySign,
      'success': function (res) {
        that.codeCancel()
      },
      'fail': function (res) {
        wx.showModal({
          title: '提示',
          content: '支付失败',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
            }
          }
        })
      }
    })
  },
  // 取消
  codeCancel: function(){
    this.setData({
      qrcode: true
    })
  },
  // 确定
  codeConfirm: function(){
    this.payMoney()
  },
  cancel: function(){
    this.setData({
      dateFlag: ''
    })
    wx.navigateBack()
  },
  confirm: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'shopOrder/payTheBillToGY',
      data: { backupE: _this.data.ShopId, userId: _this.data.userInfo.id, openId: app.globalData.openId },
      success: function (res) {
        if (res.data.success) {
          _this.requestPayment1(res.data.data);
        }
      }
    })
  },
  requestPayment1: function (obj) {
    var that = this;
    wx.requestPayment({
      'timeStamp': obj.timeStamp,
      'nonceStr': obj.nonceStr,
      'package': obj.package,
      'signType': obj.signType,
      'paySign': obj.paySign,
      'success': function (res) {
        wx.navigateBack()
      },
      'fail': function (res) {
        wx.navigateBack()
      }
    })
  },
  // 打电话
  toggleSelect: function (e) {
    var show = e.currentTarget.dataset.value == "true" ? true : false;
    this.setData({
      showSelect: show,
      current_tel: e.currentTarget.dataset.tel
    })
  },
  callPhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.value
    })
    this.setData({
      showSelect: false
    })
  },
  // 获取店铺所有商品信息
  getAllgoods: function(id){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'shopThing/page',
      data: {
        shopId: _this.data.id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        for (var i = 0; i < res.data.data.pp.length; i++) {
          res.data.data.pp[i].realThingBanerImg = res.data.data.pp[i].realThingBanerImg.split(",");
          res.data.data.pp[i].realThingProductsImg = res.data.data.pp[i].realThingProductsImg.split(",");
          res.data.data.pp[i].realThingShowImg = res.data.data.pp[i].realThingShowImg.split(",");
        }
        _this.setData({
          commodity: res.data.data.pp
        })
      }
    })
  },
  // 获取店铺首页商品信息
  getIndexgoods: function (id) { 
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'shopThing/allHotThing',
      data: {
        shopId: _this.data.id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        for (var i = 0; i < res.data.data.length; i++) {
          res.data.data[i].realThingShowImg = res.data.data[i].realThingShowImg.split(",");
        }
        _this.setData({
          commodity: res.data.data
        })
      }
    })
  },
  // 获取新品上架商品信息
  getNewgoods: function (id) {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'shopThing/page',
      data: {
        shopId: _this.data.id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        for (var i = 0; i < res.data.data.pp.length; i++) {
          res.data.data.pp[i].realThingBanerImg = res.data.data.pp[i].realThingBanerImg.split(",");
          res.data.data.pp[i].realThingProductsImg = res.data.data.pp[i].realThingProductsImg.split(",");
          res.data.data.pp[i].realThingShowImg = res.data.data.pp[i].realThingShowImg.split(",");
        }
        var arr = [];
        for(var j = 0; j < 6; j++){
          arr.push(res.data.data.pp[j])
        }
        if (res.data.data.pp.length > 6){
          _this.setData({
            commodity: arr
          })
        }
        else{
          _this.setData({
            commodity: res.data.data.pp
          })
        }
      }
    })
  },
  openDetail: function(e){
    wx.setStorageSync('code', e.currentTarget.dataset.img);
    var area = e.currentTarget.dataset.area;
    wx.navigateTo({
      url: '/pages/wareDetail/wareDetail?id=' + e.currentTarget.dataset.id + '&isMine=' + this.data.isMine + '&shopId=' + this.data.id + '&area=' + area
    })
  },
  // 收藏
  collect: function (e) {
    var _this = this;
    if(e.currentTarget.dataset.coll == 'N'){
      wx.request({
        url: app.globalData.httpApiCar + 'tGyCollection/save',
        data: {
          userId: _this.data.userInfo.id,
          sourceId: _this.data.id,
          block: 1
        },
        method:'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if(res.data.success){
            _this.setData({
              isCollect: 'Y',
              collectId: res.data.data
            })
          }
        }
      })
    }
    else{
      wx.request({
        url: app.globalData.httpApiCar + 'tGyCollection/deleteCollectionRecordByParams',
        data: {
          userId: _this.data.userInfo.id,
          sourceId: _this.data.id,
          block: 1
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            _this.setData({
              isCollect: 'N'
            })
          }
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },
  changeCart:function(e){
    if (this.data.commodity[e.currentTarget.dataset.value].isCart==true){
      this.data.commodity[e.currentTarget.dataset.value].isCart =false;
    }else{
      this.data.commodity[e.currentTarget.dataset.value].isCart = true;
    }
    this.setData({
      commodity: this.data.commodity
    })
  },
  bingfocusFunc: function (e) {
    this.setData({
      focus: 'focus'
    })
  },
  bingblurFunc: function (e) {
    
    this.setData({
      focus: ''
    })
  },
  tabSwitch: function (e) {
    let index = e.currentTarget.dataset.value;
    this.data.viewtabList.forEach(function(ele){
      ele.current=false;
    })
    this.data.viewtabList[e.currentTarget.dataset.value].current = true;
    this.setData({
      viewtabList: this.data.viewtabList
    })
    if(index == 0){
      this.getIndexgoods()
    }
    else if(index == 1){
      this.getAllgoods()
    }
    else if(index == 2){
      this.getNewgoods()
    }
  },
  callPhone:function(e){
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.value 
    })
    this.setData({
      showSelect: false
    })
  },
  toggleSelect: function (e) {
   var show = e.currentTarget.dataset.value=="true"?true:false;
   this.setData({
     showSelect: show
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})